# https://www.cnblogs.com/poloyy/p/15260379.html
from datetime import datetime

from pydantic import BaseModel
from typing import Optional, List


class User(BaseModel):
    id: int
    name: str = "lxq"  # 有默认值，选填字段
    signup_ts: Optional[datetime] = None
    friends: List[int] = []  # 列表中元素是 int 类型，或可以直接转成 int 的类型


user = User(id="1", name="大菠萝", signup_ts="2021-09-16 12:22:00")

print(user.dict())

data = {
    "id": "2",
    "name": "大大的菠萝",
    "friends": [1, 2, 3]
}

user2 = User(**data)
print(user2.dict())

print("======================================================================================")


class Foo(BaseModel):
    count: int
    size: float = None


class Bar(BaseModel):
    apple = 'x'
    banana = 'y'


class Spam(BaseModel):
    foo: Foo
    bars: List[Bar]

f = Foo(count=2)
b = Bar()
c = Bar(apple='lxq', banana='dm')
s = Spam(foo=f, bars=[b, c])

print(s.json())